package com.atguigu.exer;

import org.junit.Test;

import java.util.Scanner;

/**
 * 将一个字符串进行反转，将字符串中指定部分进行反转，如："abcdefg" --> "abfedcg"。
 */
public class StringDemo {

    //方式一：
    public String Reverse(String str, int startIndex, int endIndex) {
        char[] arr = str.toCharArray();
        for (int x = startIndex, y = endIndex; x < y; x++, y--) {
            char c = arr[x];
            arr[x] = arr[y];
            arr[y] = c;
        }
        String arr2 = new String(arr);
        return arr2;
    }

    @Test
    public void test1(){
        String str = "abcdef";
        String reverse = Reverse(str, 2, 5);
        System.out.println(reverse);
    }

    public static void  main(String[] args) {
        //自写
        Scanner scan = new Scanner(System.in);
        System.out.print("请输入一个字符串：");
        String s1 = scan.next();
        char[] c1 = s1.toCharArray();

        for (int i = 2, j = s1.length() - i; i < j; i++, j--) {
            char c2 = c1[i];
            c1[i] = c1[j];
            c1[j] = c2;
        }
        String s2 = new String(c1);
        System.out.println(s2);
    }

}
